const app = getApp()
const ImgLoader = require('../../img-loader/img-loader.js')
Page({
     data: {
          //轮播图
          imgUrls: [

          ],
          currentTab: 0,
          height1: 0,
          height2: 0,
          recommend_data: [], //推荐数据
          r_page: 1, //推荐数据 页码
          newest_data: [], //最新数据
          n_page: 1, //最新数据页码
          userInfo: {},
          isLogin: false, //是否登陆
          isMobile: false, //是否绑定手机号
          setting: {},
          fixed:false
     }, 
     //加载完成后的回调 推荐
     imageOnLoad(err, data) {
     
          let that = this;
          let recommend_data = that.data.recommend_data;
          for (let item in recommend_data) {
               for (let item1 in recommend_data[item].imgs) {
                    if (recommend_data[item].imgs[item1].pic_path == data.src) {
                         var up = "recommend_data[" + item + "].imgs[" + item1 + "].loaded";
                         that.setData({
                              [up]: true
                         })
                    }
               }
          }
          //最新
          let newest_data = that.data.newest_data;
          for (let item in newest_data) {
               for (let item1 in newest_data[item].imgs) {
                    if (newest_data[item].imgs[item1].pic_path == data.src) {
                         var up = "newest_data[" + item + "].imgs[" + item1 + "].loaded";
                         that.setData({
                              [up]: true
                         })
                    }
               }
          }
     },
  
     onLoad: function () {
          wx.hideTabBar();
          let that = this;
          //懒加载图片
          this.imgLoader = new ImgLoader(this, this.imageOnLoad.bind(this))

          //加载轮播图
          app.connect('index/banner', [], function (options) {
               that.setData({
                    imgUrls: options
               });
          });
          app.connect('index/setting', {}, function (res) {
               that.setData({
                    setting: res
               })
          });
          app.login_canno(function (userInfo) {
               if (userInfo != null) {
                    that.setData({
                         userInfo: userInfo.data,
                         isLogin: true
                    });
                    if (app.isPoneAvailable(userInfo.data.mobile)) {
                         that.setData({
                              isMobile: true
                         })
                    }
               }

               //加载首页推荐信息

               var userLogin = [];
               if (that.data.userInfo.uid != null && that.data.userInfo.openId != null) {
                    userLogin['uid'] = that.data.userInfo.uid;
                    userLogin['openid'] = that.data.userInfo.openId;
               }
               app.connect('content/index', userLogin, function (options) {
                    that.setData({
                         recommend_data: options.data
                    });
                    //获取节点高度
                    that.jiedian();
               }, true);

               //加载首页最新数据
               app.connect('content/newest', userLogin, function (options) {
                    that.setData({
                         newest_data: options.data
                    });

                    //获取节点高度
                    that.jiedian();
               }, false);
               console.log(that.data);
          });

     },
     //获得推荐和最新部分的节点高度
     jiedian: function () {
          let that = this;
         
          var query = wx.createSelectorQuery()
          query.select('#tuijian').boundingClientRect()
          query.selectViewport().scrollOffset()
          query.exec(function (res) {
               that.setData({
                    height1: res[0].height
               });
               // // //推荐列表图片懒加载
               // that.data.recommend_data.forEach(item => {
               //      item.imgs.forEach(item => {
               //           that.imgLoader.load(item.pic_path)
               //      });
               // })
          })
          var query = wx.createSelectorQuery()
          query.select('#zuixin').boundingClientRect()
          query.selectViewport().scrollOffset()
          query.exec(function (res) {
               that.setData({
                    height2: res[0].height
               });
               // //最新
               // that.data.newest_data.forEach(item => {
               //      item.imgs.forEach(item => {
               //           that.imgLoader.load(item.pic_path)
               //      });
               // })
          }) 

         
     },
     onShareAppMessage: function () {
          return {
               title: '麦颜值',
               path: '/pages/index/index'
          }
     },
     location: function (option) {
          var type = option.currentTarget.dataset.type;
          var appid = option.currentTarget.dataset.appid;
          var path = option.currentTarget.dataset.path;
          //跳转到小程序内页面
          if (type == 1) {
               wx.reLaunch({
                    url: path,
                    fail: function (e) {

                    }
               })
          }
     },

     bindChange: function (e) {

          var that = this;
          that.setData({ currentTab: e.detail.current });
    
     },
	/**
	 * 点击tab切换
	 */
     swichNav: function (e) {
          var that = this;

          if (this.data.currentTab === e.target.dataset.current) {
               return false;
          } else {
               that.setData({
                    currentTab: e.target.dataset.current
               })
          }

     },
     onReachBottom: function () {
          let that = this;
          //推荐数据页面
          var r_page = that.data.r_page;
          //最新数据页码
          var n_page = that.data.n_page;
          var currentTab = that.data.currentTab;
          wx.showLoading();
          var userLogin = [];
          if (that.data.userInfo.uid != null && that.data.userInfo.openId != null) {
               userLogin['uid'] = that.data.userInfo.uid;
               userLogin['openid'] = that.data.userInfo.openId;

          }
          //推荐数据
          if (currentTab == 0) {
               userLogin['page'] = r_page + 1;
               app.connect('content/index', userLogin, function (options) {
                    if (options.data.length == 0) {
                         wx.showToast({
                              title: '到底啦',
                              icon: 'none'
                         })
                         return false;
                    }
                    var recommend_data = that.data.recommend_data;
                    that.setData({
                         recommend_data: recommend_data.concat(options.data)
                    });
                    //获取节点高度
                    that.jiedian();
                    wx.hideLoading()
               }, false);
               that.setData({
                    r_page: r_page + 1
               });
          }
          //最新数据
          else if (currentTab == 1) {
               userLogin['page'] = n_page + 1;
               app.connect('content/newest', userLogin, function (options) {
                    if (options.data.length == 0) {
                         wx.showToast({
                              title: '到底啦',
                              icon: 'none'
                         })
                         return false;
                    }
                    var newest_data = that.data.newest_data;
                    that.setData({
                         newest_data: newest_data.concat(options.data)
                    });
                    //获取节点高度
                    that.jiedian();
                    wx.hideLoading()
               }, false);
               that.setData({
                    n_page: n_page + 1
               });
          }
     },
	/**
	 * 页面滚动监听
	 */
     onPageScroll: function (e) {
          let that = this;
          var top = e.scrollTop;
          var query = wx.createSelectorQuery()
          query.select('#swiper').boundingClientRect()
          query.selectViewport().scrollOffset()
          query.exec(function (res) {
               var height = res[0].height;
               if (top >= height) {
                    that.setData({
                         fixed: true
                    })
               } else {
                    that.setData({
                         fixed: false
                    })
               }
          })
     },
     onPullDownRefresh: function () {
          this.onLoad();
          this.setData({
               r_page: 1, 
               n_page:1
          });
          wx.stopPullDownRefresh()
     },
     //转发
     onShareAppMessage: function (res) {
          let that = this;
          if (res.from == 'menu') {
               return {
                    title: '帅哥美女潮人红人都在玩',
                    path: 'pages/index/index',
               }
          }
          var content_id = res.target.dataset.content_id;
          var imgUrl = res.target.dataset.imgurl;
          var nick = res.target.dataset.nick;
          console.log(res);
          return {
               title: '@' + nick + ' 在麦颜值发表的作品',
               path: 'pages/look/index?content_id=' + content_id + '&type=1',
               imageUrl: imgUrl
          }
     },
     lookImg: function (e) {

          var content_id = e.currentTarget.dataset.content_id;
          var img_id = e.currentTarget.dataset.img_id;
          wx.navigateTo({
               url: '../look/index?content_id=' + content_id + "&img_id=" + img_id,
          })
     }
})